package com.story.story_serve.service.Book;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.story.story_pojo.Book.Bmypage;
import com.story.story_pojo.Book.Book;
import com.story.story_pojo.Book.BookDetails;
import com.story.story_serve.mapper.Book.BookMapper;
import com.story.story_serve.mapper.Book.BookDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName:  BookService
 * description:
 *
 * @ author:郭海龙
 * @ Create：2024/6/2 20:08
 * @ Version:1.0
 */
@Service
public class BookService {
    @Autowired
    BookMapper bookMapper;

    @Autowired
    BookDetailMapper bookDetailMapper;

    // 前12本
    public List<Book> getTop12Book() {
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("b_id");
        queryWrapper.last("limit 12");
        return bookMapper.selectList(queryWrapper);
    }

    // 翻页数据
    public Map<String, Object> pageBook(Bmypage bmypage) {
        QueryWrapper<Book> qw = new QueryWrapper<>();
        // 类型分类
        if (bmypage.getBType() != null && !"".equals(bmypage.getBType())) {
            qw.like("b_type", bmypage.getBType());
        }
        // 国家分类
        else if (bmypage.getBStatus() != null && !"".equals(bmypage.getBStatus())) {
            qw.like("b_status", bmypage.getBStatus());
        }
        // 搜索
        else if (bmypage.getSearch() != null && !"".equals(bmypage.getSearch())) {
            qw.like("b_name", bmypage.getSearch());
            qw.or().like("b_author", bmypage.getSearch());
        }
        // 创建一个分页对象   参数1：当前页     参数2：每页数量
        Page<Book> page = new Page<>(bmypage.getPage(), bmypage.getSize());
        // 调用持久层实现分页  查完结果返回到page对象
        page = bookMapper.selectPage(page, qw);
        // 获取总页数
        Long totalPage = page.getPages();
        // 获取每页显示的数量
        Long size = page.getSize();
        // 分页后的数据列表
        List<Book> books = page.getRecords();

        // 封装查询结果
        Map<String, Object> pageMap = new HashMap<>();
        pageMap.put("totalPage", totalPage);
        pageMap.put("size", size);
        pageMap.put("books", books);

        return pageMap;


    }

    // 根据id找小说
    public Book getBookById(Book book) {
        QueryWrapper<Book> qw = new QueryWrapper<>();
        qw.eq("b_id", book.getBId());
        return bookMapper.selectOne(qw);

    }

    // 目录
    public List<BookDetails> getDetailsById(Book book) {
        QueryWrapper<BookDetails> qw = new QueryWrapper<>();
        qw.eq("b_id",book.getBId());
        return bookDetailMapper.selectList(qw);
    }
}
